package com.tangbuzhi.chm.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.ImageView;

import com.tangbuzhi.chm.R;
import com.tangbuzhi.chm.adapter.ChannelAdapter;
import com.tangbuzhi.chm.helper.TouchCallback;
import com.tangbuzhi.chm.model.Channel;
import com.tangbuzhi.chm.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * http://git.oschina.net/tangbuzhi
 *
 * @author Tangbuzhi
 * @version V2.0
 * @Package com.tangbuzhi.chm.ui
 * @Description: ${TODO}
 * @date: 2017/9/11
 */
public class ChannelActivity extends AppCompatActivity {

    private RecyclerView recycler;
    private ImageView close;
    private List<Channel> channels, others;
    private ChannelAdapter adapter;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_channel);

        recycler = (RecyclerView) findViewById(R.id.recycler);
        close = (ImageView) findViewById(R.id.close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData();
                finish();
            }
        });

        channels = new ArrayList<>();
        others = new ArrayList<>();

        String[] strings = getIntent().getStringArrayExtra("channel");
        for (int i = 0; i < strings.length; i++) {
            this.channels.add(new Channel(strings[i], i + 1));
        }

        for (int i = 0; i < 10; i++) {
            others.add(new Channel("其他" + (i + 1), i + 1));
        }

        GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
        recycler.setLayoutManager(layoutManager);

        TouchCallback callback = new TouchCallback();
        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
        touchHelper.attachToRecyclerView(recycler);

        adapter = new ChannelAdapter(this, touchHelper, this.channels, others);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int itemViewType = adapter.getItemViewType(position);
                return itemViewType == ChannelAdapter.TYPE_CHANNEL || itemViewType == ChannelAdapter.TYPE_OTHER ? 1 : 4;
            }
        });
        recycler.setAdapter(adapter);
        adapter.setOnChannelItemClickListener(new ChannelAdapter.OnChannelItemClickListener() {
            @Override
            public void channelItemClick(int position, String name) {
                ToastUtil.showToast(context, name);
            }
        });
    }


    private void initData() {
        int size = channels.size();
        String[] names = new String[size];
        List<String> channels = adapter.getChannels();
        for (int i = 0; i < size; i++) {
            names[i] = channels.get(i);
        }
        Intent intent = new Intent();
        intent.putExtra("chaannel", names);
        setResult(RESULT_OK, intent);
    }

    @Override
    public void onBackPressed() {
        // TODO: 2017/9/11 adapter在编辑模式时，取消编辑模式再back
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        context = null;
    }
}
